// pages/recruit/application.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      items:'',
      item:'',
      nameOfAppl:'',
      schoolOfAppl:'',
      stunumOfAppl:'',
      departmentOfAppl:'',
      majorOfAppl:'',
      gradeOfAppl:'',
      mobileOfAppl:'',
      weixinOfAppl:'',
      resume:'',
      leaderOfPrg:'',
      collegeOfPrg:'',
      nameOfPrg:'',
      userinfo:'',
      chuangNum:'',
      sessionID:'',
      html:''
    },
    gotoRevision(e) {
        wx.navigateTo({
          url: '/pages/recruit/revision/revision?idOfPrg='+this.data.idOfPrg,
        })  

      },

      gotoConfirm(e) {
        wx.request({
          url: 'https://www.hschuangsai.com/message/apply?projectId='+this.data.idOfPrg,
          method: 'GET',
          header: {
              'content-type': 'application/json',
              'session': this.data.sessionID
          },
          success:(gotoconfirm)=>{
            console.log('Confirm',gotoconfirm)
            if (gotoconfirm.data.code==-1){
              wx.showToast({
                title: '请先进入“我的”填写基础简历！',
                icon: 'none',
                duration:1500
              })
              setTimeout(function () {
                wx.navigateTo({
                  url:'../../account/resume/resume'
              })
              }, 1500)
          }
          if (gotoconfirm.data.code==0){
            wx.showToast({
              title: '申请成功',
              icon: 'none',
              duration:1500
            })
            setTimeout(
              wx.navigateTo({
                url: '/pages/recruit/introduction/introduction?idOfPrg='+this.data.idOfPrg
            })
             ,1500)
        }
        }
        })
      },

      buildInfo(e){

      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

      this.setData({
        nameOfAppl:"",
        schoolOfAppl:'',
        stunumOfAppl:'',
        departmentOfAppl:'',
        majorOfAppl:'',
        gradeOfAppl:'',
        mobileOfAppl:'',
        weixinOfAppl:'',
        resume:'',
        leaderOfPrg:'',
        collegeOfPrg:'',
        nameOfPrg:'',
        loginFlag:0,
        idOfPrg:options.idOfPrg,
        sessionID:''
      })
      this.setData({
        userInfo: wx.getStorageSync('userInfo'), // 用户的微信头像、昵称等信息
        loginFlag: wx.getStorageSync('loginFlag'), // 判断是否登录，登录为1，未登录为0或空
        sessionID: wx.getStorageSync('sessionID'),
        chuangNum: wx.getStorageSync('chuangNum'),
      })
    if (this.data.loginFlag != 1) {
        this.myShowModal();
    }
    wx.request({
      url:'https://www.hschuangsai.com/recruitProject/info?projectId='+this.data.idOfPrg,
      method:"GET",
      success:(res2)=>{
        if (res2.statusCode!=200) {
          wx.showToast({
              title: '获取项目信息失败，请重试',
              icon: 'none',
              duration: 800     
            })  
      }
        this.setData({
          leaderOfPrg:res2.data.data.recruitProject.captainName,
          collegeOfPrg:res2.data.data.recruitProject.institute,
          nameOfPrg:res2.data.data.recruitProject.name,
        })
      }
    })

    wx.request({
      url: 'https://www.hschuangsai.com/user/info?userChuangNum=' + this.data.chuangNum,
      method: 'GET',
      header: {
          'session': this.data.sessionID
      },
      // 请求登录态成功
      success: (res) => {
        if (res.statusCode!=200) {
          wx.showToast({
              title: '获取用户信息失败，请重试',
              icon: 'none',
              duration: 800     
            })  
      }
          console.log('获取用户信息', res)
          this.setData({
            nameOfAppl:res.data.data.name,
            schoolOfAppl:res.data.data.school,
            stunumOfAppl:res.data.data.studentNumber,
            departmentOfAppl:res.data.data.department,
            majorOfAppl:res.data.data.major,
            gradeOfAppl:res.data.data.grade,
            mobileOfAppl:res.data.data.telephone,
            weixinOfAppl:res.data.data.wechatNum,
            resume:res.data.data.resume,
            attachment:res.data.data.attachment,
            attachmentName:decodeURI(res.data.data.attachmentName),
            attachment2:res.data.data.attachment2,
            attachment2Name:decodeURI(res.data.data.attachment2Name),
            attachment3:res.data.data.attachment3,
            attachment3Name:decodeURI(res.data.data.attachment3Name),
          })
          var WxParse = require('../../wxParse/wxParse.js');
          var data=this.data;
          WxParse.wxParse('content', 'html', data.html, this, 10);
          if (this.data.nameOfAppl=='')
          {
            wx.showToast({
              title: '请先进入“我的”填写基础简历！',
              icon: 'none',
              duration:1500
            })
            setTimeout(function () {
              wx.navigateTo({
                url:'../../account/resume/resume'
            })
            }, 1500)
          }
        }
    })
  },

  openDocument1:function(e){
    let suffix='';
    const fileArr=this.data.attachment.split('.');
    suffix=fileArr[fileArr.length-1];
    console.log("the suffix is"+suffix)
    wx.downloadFile({
        url: this.data.attachment,
        filePath: wx.env.USER_DATA_PATH + '/' + this.data.attachmentName,
        success: function (res) {
            console.log(res)
            if (res.statusCode == 200) {
                let filePath = res.filePath
                wx.openDocument({
                    fileType: suffix,
                    filePath: filePath
                })
            }
            else{
              wx.showToast({
                  title: '打开文件失败，请重试',
                  icon: 'none',
                  duration: 800     
                })  
          }
        }
    })
},
openDocument2:function(e){
    let suffix='';
    const fileArr=this.data.attachment3.split('.');
    suffix=fileArr[fileArr.length-1];
    console.log("the suffix is"+suffix)
    console.log("the attachment2 is"+this.data.attachment2)
    wx.downloadFile({
        url: this.data.attachment2,
        filePath: wx.env.USER_DATA_PATH + '/' + this.data.attachment2Name,
        success: function (res) {
            console.log(res)
            if (res.statusCode == 200) {
                let filePath = res.filePath
                wx.openDocument({
                    fileType: suffix,
                    filePath: filePath
                })
            }
            else{
              wx.showToast({
                  title: '打开文件失败，请重试',
                  icon: 'none',
                  duration: 800     
                })  
          }
        }
    })
},


openDocument3:function(e){
    let suffix='';
    const fileArr=this.data.attachment3.split('.');
    suffix=fileArr[fileArr.length-1];
    console.log("the suffix is"+suffix)
    wx.downloadFile({
        url: this.data.attachment3,
        filePath: wx.env.USER_DATA_PATH + '/' + this.data.attachment3Name,
        success: function (res) {
            console.log(res)
            if (res.statusCode == 200) {
                let filePath = res.filePath
                wx.openDocument({
                    fileType: suffix,
                    filePath: filePath
                })
            }
            else{
              wx.showToast({
                  title: '打开文件失败，请重试',
                  icon: 'none',
                  duration: 800     
                })  
          }
        }
    })
},
    
   myShowModal() {
    wx.showModal({
        title: '提示',
        content: '登录后查看更多，是否登录？',
        success: (res) => {
            // 确定
            if (res.confirm) {
                this.logIn();
            }
            else {
                 wx.showToast({
                     title: '跳转到首页',
                     icon: 'none',
                    duration: 1500
                 })
                 setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/recruit/recruit',
                    success:function(e)
                    {
                      var page = getCurrentPages().pop();
                      if (page == undefined || page == null) return;
                      page.onLoad();
                    }
                  })  
                }, 1500)
            }
        }
    })
  },

  logIn() {
    // 如果userInfo为空，则未确认过微信授权弹窗，即首次登录
    if (!this.data.userInfo) {
        // 微信授权弹窗
        wx.getUserProfile({
            desc: '用于获取用户头像和昵称',
            // 用户点击确认
            success: (res) => {
                let userInfo = res.userInfo;
                wx.setStorageSync('userInfo', userInfo); // 缓存userInfo
                this.myLogin(); // 进行登录
            },
            // 用户点击取消
            fail: (res) => {
                // wx.showToast({
                //     title: '跳转到首页',
                //     icon: 'none',
                //     duration: 1500
                // })
            }
        })
    }
    // 非首次登录
    else {
        this.myLogin(); // 重新登录
    }
},

// wx.login()封装
myLogin() {
    wx.login({
        success: (res) => {
            wx.request({
                url: 'https://www.hschuangsai.com/user/login/' + res.code,
                method: 'GET',
                success: (res) => {
                    console.log("myLogin success", res);
                    let sessionID = res.data.data.sessionId;
                    wx.setStorageSync('chuangNum',res.data.data.chuangNum)
                    wx.setStorageSync('sessionID', sessionID); // 缓存sessionID
                    wx.setStorageSync('loginFlag', 1); // 缓存loginFlag
                    this.setData({
                        sessionID: sessionID,
                        loginFlag: 1,
                    });

                    wx.showToast({
                        title: '成功登录，继续申请入队',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        }
    })
}, 

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(){
      var WxParse = require('../../wxParse/wxParse.js');
      var data=this.data;
      WxParse.wxParse('content', 'html', data.html, this, 10);
  },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})